package com.travel.server.manager.impl.command.user;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.user.DeleteDepartmentRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.DepartmentDal;
import com.travel.server.manager.model.DepartmentInfo;
import com.travel.server.manager.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/20 14:24
 * @Desc:
 */
@Component
public class DeleteDepartmentCommand extends AbstractCommand<DeleteDepartmentRequest, EmptyResponse> {

    @Autowired
    private DepartmentDal departmentDal;

    @Override
    protected void checkInput(DeleteDepartmentRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(DeleteDepartmentRequest request) {
        checkInput(request);
        UserInfo userInfo = getUserInfo();

        DepartmentInfo info = departmentDal.selectById(request.getId());
        info.setState(CommonConstant.NO);
        info.setUpdateBy(userInfo.getName());
        info.setUpdateTime(new Date());
        departmentDal.insertOrUpdate(info);

        return new EmptyResponse();
    }
}
